<?php
/*
	Plugin Name: Wp-Sortable-Data
	Plugin URI: http://code.google.com/p/wp-sortable-data/
	Description: This is a wordpress plugin that permits to make a sortable data list in your page. The sortable data can do in jquery and the order is saved on database for the futher view in your page.
	Version: 0.0.0
	Author: Domenico Biancardi
	Author URI: http://www.domenicobiancardi.com
 */

define('WP_SORTABLE_DATA', '0.0.0');

define('BASE_URL', "options-general.php?page=sortdata-identifier");



class Sortdata
{
	private $db;
	/**
	 * Costruttore della classe
	 */
    public function Sortdata() {
    	$this -> userAccess();
    }
	
	private function initDb(){
		global $wpdb, $tableposts;
		$this -> db = $wpdb;
	}
	/**
	 * Inizializzazione delle azioni
	 */
    public function init() {    	
    	add_action('admin_menu', array(&$this, 'sortdata_menu') );
    	$this -> initDb();
    }
    
    /**
     * Gestione del menu di amministrazione principale
     */
	public function sortdata_menu() {
		add_options_page('Sortdata Options', 'Sortable Data', 'manage_options', 'sortdata-identifier', array(&$this, 'sortdata_options'));
	}
	
	private function userAccess(){
		/*if (!current_user_can('manage_options'))  {
			wp_die( __('You do not have sufficient permissions to access this page.') );
		}*/
	}
	private function lists(){
		global $wpdb, $tableposts;
		$lists = $wpdb ->get_results("SELECT * FROM wp_sortable_list
			ORDER BY sortable_list_id DESC");
		$str = '<a href="'.BASE_URL.'&op=add">Add new list</a>
		<table id="list">';
		
		foreach ($lists as $list) {
			$str.=  "<tr><td>" .$list->sortable_list_name . "</td><td><a href='".BASE_URL."&op=edit&id=".$list->sortable_list_id."'>Edit</a></td><td><a href='".BASE_URL."&op=delete&id=".$list->sortable_list_id."'>Delete</a></td></tr>";
		}

		$str .= '</table>';
		return $str;
	}
	private function delete($id){
		
	}
	private function add($data = array()){
	
		$str = '<form action="'.BASE_URL.'&op=save" method="POST">' .
				'Nome lista: <input type="text" name="name" value="'.$data -> sortable_list_name.'"/><br/>' .
				'<input type="hidden" name="page" value="sortdata-identifier" />' .
				'<input type="hidden" name="id" value="'.$data -> sortable_list_id.'" />' .
			   '<input type="submit" name="salva" />' .
			   '<script type="text/javascript" src="/blog/wp-content/plugins/wp-sortable-data/js/jquery-1.4.2.min.js"></script> ' .
			   '<script type="text/javascript" src="/blog/wp-content/plugins/wp-sortable-data/js/jquery.tablednd_0_5.js"></script> ' .
			   '<script type="text/javascript">
				$(document).ready(function() {
				    // Initialise the table
				    $("#table-1").tableDnD();
				});
				</script>' .
			   '<table id="table-1" cellspacing="0" cellpadding="2">
			    <tr id="1"><td>1</td><td>Text: <input type="text" name="text" value=""></td></tr>
				</table>' .
			   '</form>';
		return $str;
	}
	private function edit($id){
		global $wpdb, $tableposts;
		$obj = $wpdb ->get_results("SELECT * FROM wp_sortable_list
			where sortable_list_id = '$id'");
		return $this -> add($obj[0]);
	}
	private function save(){
		global $wpdb, $tableposts;
		$id = $_POST["id"];
		if ($id != 0){
			$wpdb->update( 'wp_sortable_list', array( 'sortable_list_name' => $_POST["name"] ), array("sortable_list_id" => $id), array("%s"), array("%d"));
		}else{
			$wpdb->insert( 'wp_sortable_list', array( 'sortable_list_name' => $_POST["name"] ), array("%s"));	
		}
		
		return $this -> lists();
		
	}
	/**
	 * Pagina main
	 */
	public function sortdata_options() {
		$data = "";
		switch($_GET["op"]){
			default:
				$data = $this -> lists();
				break;
			case "add":
				$data = $this -> add();
				break;
			case "edit":
				$data = $this -> edit($_GET["id"]);
				break;
			case "save":
				$data =$this -> save();
				break;
			case "delete":
				$data =$this -> delete();
				break;
		}
		echo $data;

	}
		
}





$sortdata = new Sortdata();
$sortdata -> init();

unset($sortdata);


?>